package cn.lanqiao.bigDate.service.data.impl;

import cn.lanqiao.bigDate.entity.data.DataOrderStatus;
import cn.lanqiao.bigDate.mapper.data.DataOrderStatusMapper;
import cn.lanqiao.bigDate.service.data.DataOrderStatusService;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class DataOrderStatusImpl implements DataOrderStatusService {

    @Resource
    private DataOrderStatusMapper dataOrderStatusMapper;

    /**
     * 分页
     * @param page
     * @param searchOrderNumber
     * @return
     */
    @Override
    public IPage<DataOrderStatus> findOrderStatusByPage(Page<DataOrderStatus> page, String searchOrderNumber){
        QueryWrapper<DataOrderStatus> wrapper = new QueryWrapper<>();
        wrapper.like("order_number",searchOrderNumber);
        Page<DataOrderStatus> dataOrderStatusPage = dataOrderStatusMapper.selectPage(page,wrapper);
        return dataOrderStatusPage;
    }

    /**
     * 按订单编号查询
     * @param orderNumber
     * @return
     */
    @Override
    public DataOrderStatus findOrderNumber(int orderNumber) {
        QueryWrapper<DataOrderStatus> wrapper = new QueryWrapper<>();
        wrapper.eq("order_number",orderNumber);
        DataOrderStatus dataOrderStatus = dataOrderStatusMapper.selectOne(wrapper);
        return dataOrderStatus;
    }

    /**
     * 按订单编号删除
     * @param numbers
     * @return
     */
    @Override
    public Boolean deleteOrderNumber(String numbers) {
        if(!StringUtils.isEmpty(numbers)){
            String[] number = numbers.split(",");
            List<String> numList = Arrays.asList(number);
            int i = dataOrderStatusMapper.deleteBatchIds(numList);
            return i > 0;
        }
        return false;
    }

    /**
     * 添加订单信息
     * @param dataOrderStatus
     * @return
     */
    @Override
    public Boolean addOrderStatusData(DataOrderStatus dataOrderStatus) {
        dataOrderStatus.setCreateTime(LocalDateTime.now());
        int insert = dataOrderStatusMapper.insert(dataOrderStatus);
        if(insert == 1){
            return true;
        }
        return false;
    }

    /**
     * 修改订单信息
     * @param dataOrderNumber
     * @return
     */
    @Override
    public Boolean updateOrderByNumber(DataOrderStatus dataOrderNumber) {
        if(dataOrderNumber !=null&& dataOrderNumber.getOrderNumber()!=null){
            String Number = dataOrderNumber.getOrderNumber();
            int num = Integer.parseInt(Number);
            DataOrderStatus dataOrderStatus = findOrderNumber(num);
            dataOrderStatus.setOrderNumber(dataOrderNumber.getOrderNumber());
            dataOrderStatus.setDate(dataOrderNumber.getDate());
            dataOrderStatus.setStatus(dataOrderNumber.getStatus());
            dataOrderStatus.setInformation(dataOrderNumber.getInformation());
            dataOrderStatus.setCreateTime(dataOrderNumber.getCreateTime());
            dataOrderStatus.setCreateBy(dataOrderNumber.getCreateBy());
            dataOrderStatus.setUpdateTime(dataOrderNumber.getUpdateTime());
            dataOrderStatus.setUpdateBy(dataOrderNumber.getUpdateBy());
            int i = dataOrderStatusMapper.updateById(dataOrderNumber);
            if(i==1){
                return true;
            }
            return false;
        }
        return false;
    }


}
